<!-- 耗能情况指标 -->
<template>
  <div class="ctn-box ctn-box-4">
    <div class="ctn-box-title">{{params?.monthParams}}月耗能情况</div>
    <div class="ctn-box-base">
      <div class="box">
        <img src="@/assets/icons/energy/icon1.png"/>
        <div class="box-details">
          <div class="box-details-row bdr-1">
            <span class="sp1"><em>总电费</em><i>￥{{caleData(baseInfo?.zdf) ?? '--'}}</i></span>
            <span class="sp1"><em>较同期</em><i class="i-up">{{baseInfo?.jtqdf ?? '--'}}</i></span>
          </div>
          <div class="box-details-row">
            <span class="sp2">光伏电费:<i>{{caleData(baseInfo?.gfdf) ?? '--'}}</i></span>
            <span class="sp2">南方电网电费:<i>{{caleData(baseInfo?.nfdwdf) ?? '--'}}</i></span>
          </div>
        </div>
      </div>
      <div class="box">
        <img src="@/assets/icons/energy/icon2.png"/>
        <div class="box-details">
          <div class="box-details-row bdr-1">
            <span class="sp1"><em>总用电</em><i>{{baseInfo?.zyd ?? '--'}}wW.h</i></span>
            <span class="sp1"><em>较同期</em><i class="i-up">{{baseInfo?.jtqzyd ?? '--'}}</i></span>
          </div>
          <div class="box-details-row">
            <span class="sp2">光伏用电:<i>{{baseInfo?.gfyd ?? '--'}}</i></span>
            <span class="sp2">南方电网:<i>{{baseInfo?.nfdw ?? '--'}}</i></span>
          </div>
        </div>
      </div>
      <div class="pay-box">
        <div class="pay-box-item">
          <div class="sp1">
            <span>较同期</span>
            <i class="i-up">{{baseInfo?.jtqsf ?? '--'}}</i>
          </div>
          <span>水费: <i>￥{{caleData(baseInfo?.sf) ?? '--'}}</i></span>
        </div>
        <div class="pay-box-item">
          <div class="sp1">
            <span>较同期</span>
            <i class="i-up">{{baseInfo?.jtqtrqf ?? '--'}}</i>
          </div>
          <span>天然气费: <i>￥{{caleData(baseInfo?.trqf?.split('￥')[1].replace(',','')) ?? '--'}}</i></span>
        </div>
      </div>
      <div class="echarts-lines">
        <basic-echarts-bar
            :chartData="chartDataBox"
        ></basic-echarts-bar>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import BasicEchartsBar from "@/components/basicEchartsBar.vue";
import {IBase} from "@/api/interface/base";
import {caleData} from "@/utils/baseCfg";
import {getEnergyAmountMonthApi, getFindEnergyDataListApi} from "@/api/modules/base";
import {ref} from "vue";

defineOptions({
  name: 'energyView'
})
const params = defineProps({
  monthParams: {
    type: String,
    default: ''
  }
})
// 杂费报表
const chartDataBox = reactive({});
// 基础信息
let baseInfo: any = {} = ref({})

const drawEcharts = async () =>{
  const {data} = await getFindEnergyDataListApi({year: params.monthParams?.split('-')[0]})
  chartDataBox.data = {
    0: {
      data: data.map(d=>d.totalElectricityBill),
      type: 'line',
      lineColor: 'rgba(255, 194, 30, 1)', // 折现颜色
      colors: ['rgba(255, 194, 30, .2)','rgba(255, 194, 30, .01)']
    },
    1: {
      data:data.map(d=>d.monthlyWaterFee),
      type: 'line',
      lineColor: 'rgba(64, 98, 200, 1)', // 折现颜色
      colors: ['rgba(64, 98, 200, 0.2)','rgba(64, 98, 200, 0.01)']
    },
    2: {
      data:data.map(d=>d.naturalGasCost),
      type: 'line',
      lineColor: 'rgba(91, 205, 96, 1)', // 折现颜色
      colors: ['rgba(91, 205, 96, 0.2)','rgba(91, 205, 96, 0.01)']
    }
  }
  chartDataBox.label = data.map(d=>d.month + '月')
  chartDataBox.name = ["电费",'水费','天然气']
}
// 获取后端接口
const getData = async () => {
  const paramsP = reactive<IBase.Params>({
    month: parseInt(params.monthParams?.split('-')[1]),
    year: params.monthParams?.split('-')[0]
  })
  const {data} = await getEnergyAmountMonthApi(paramsP)
  baseInfo.value = data
}
watch(()=>params.monthParams,(newValue,oldValue)=>{
  // 如果不是同一年的话再更新折现图
  if(newValue?.split('-')[0] !== oldValue?.split('-')[0]){
    drawEcharts()
  }
  getData()
})

onMounted(()=>{
  drawEcharts()
})
</script>

<style lang="scss" scoped>
.ctn-box-base{
  padding: 0 11px;
  .box{
    width: 100%;
    height: 100px;
    margin: 8px 0;
    img{
      width: 80px;
      height: 79px;
    }
    .box-details{
      flex: 1;
      min-width: auto;
    }
  }
  .pay-box{
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 223px;
    .pay-box-item{
      position: relative;
      width: 198px;
      height: 100%;
      background: url("@/assets/icons/energy/icon3.png") center/100% 100% no-repeat;
      font-size: 14px;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding-top: 33px;
      &:last-child{
        background: url("@/assets/icons/energy/icon4.png") center/100% 100% no-repeat;
      }
      .sp1{
        width: 153px;
        height: 24px;
        background: url("@/assets/icons/energy/icon5.png") center/100% 100% no-repeat;
        display: flex;
        justify-content: center;
        i{
          display: inline-block;
          position: relative;
          font-style: initial;
          margin-left: 8px;
          font-family: din-bold;
          line-height: 35px;
          text-indent: 15px;
          &:before{
            position: absolute;
            left: 0;
            content: "";
            width: 14px;
            height: 100%;
          }
          &.i-up{
            &:before{
              background: url("@/assets/icons/up.png") center/100% 14px no-repeat;
            }
          }
          &.i-down{
            &:before{
              background: url("@/assets/icons/down.png") center/100% 14px no-repeat;
            }
          }
        }
      }
      >span{
        font-size: 18px;
        margin-top: 109px;
        i{
          font-style: inherit;
          color: rgba(255, 190, 65, 1);
          font-family: din-bold;;
        }
      }
    }
  }
  .echarts-lines{
    width: 100%;
    height: calc(100% - 450px);
  }

  .box-details-row{
    padding: 0 !important;
    i{
      font-size: 16px !important;
    }
  }
}
</style>
